package com.lzy.cloud.mq.controller;

import com.lzy.cloud.mq.remote.dtos.MsgOrderRefundDto;
import com.lzy.cloud.mq.remote.dtos.PayBackDTO;
import com.lzy.cloud.mq.service.OrderSender;
import com.lzy.cloud.mq.service.PaymentSender;
import com.lzy.cloud.mq.service.XXXSender;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description: lzy-cloud-service
 *
 * @author: ellengou
 * @date: 2018-07-19 星期四
 * @time: 17:19
 */
@RestController
@RequestMapping("/mq/send")
@Slf4j
public class RabbitSenderController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    XXXSender sender;
    @Autowired
    OrderSender orderSender;
    @Autowired
    PaymentSender paymentSender;

    @RequestMapping("/{ex}/{key}")
    public Result testSend(@PathVariable String ex,@PathVariable String key, Object msg){
        try {
            if (StringUtil.trimToNull(ex) != null) {
                rabbitTemplate.convertAndSend(ex,key,msg);
            }else {
                rabbitTemplate.convertAndSend(key,msg);
            }
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result();
    }

    @RequestMapping("/{key}")
    public Result testSend(@PathVariable String key, Object msg){
        try {
                rabbitTemplate.convertAndSend(key,msg);
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result();
    }

    @RequestMapping("/test")
    public Result testSend( String msg){
        try {
            sender.sender(msg);
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result();
    }

    /**
     * 订单超时30分钟未支付取消订单
     * @param orderId
     * @return
     */
    @RequestMapping("/order/cancel/{orderId}")
    public Result orderCancel(@PathVariable String orderId){
        try {
            orderSender.sender(orderId);
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result();
    }

    /**
     * 支付回调业务处理（包括支付成功和支付失败）
     * @param payBackDTO
     * @return
     */
    @RequestMapping(value = "/payment/processing",method = RequestMethod.POST)
    public Result paymentProcessing(PayBackDTO payBackDTO){
        Object obj = null;
        try {
            obj = paymentSender.sender(payBackDTO);
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result(obj);
    }

    /**
     * 退订退款（余额支付退款 + 在线支付退款）
     * @param msgOrderRefundDto
     * @return
     */
    @RequestMapping(value = "/unsubscribe",method = RequestMethod.POST)
    public Result unsubscribe(MsgOrderRefundDto msgOrderRefundDto){
        Object obj = null;
        try {
            obj = paymentSender.unsubscribeSender(msgOrderRefundDto);
        }catch (Exception e){
            log.info(e.getMessage());
            Ensure.that(e).isNull("20010001");
        }
        return new Result(obj);
    }
}
